home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / tcp_ip / timed / rdate.c < prev    next >
Text File  |  1992-04-01  |  2KB  |  98 lines

  1. /* Internet rdate client
  2.  * Author: Brian Teravskis, WD0EFL
  3.  * Date: 03/30/92
  4.  *
  5.  * Based on RFC868 Time Protocol
  6.  */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "global.h"
  10. #include "mbuf.h"
  11. #include "socket.h"
  12. #include "session.h"
  13. #include "proc.h"
  14. #include "netuser.h"
  15. #include "commands.h"
  16. #include "tty.h"
  17. #include "timer.h"
  18. #include <time.h>
  19.  
  20. #define IPPORT_TIME 37
  21. #define DIFFTIME 2208988800
  22. #define RDATETIMEOUT 30
  23.  
  24. static void rdate_timeout();
  25.  
  26. int
  27. dordate(argc,argv,p)
  28. int argc;
  29. char *argv[];
  30. void *p;
  31. {
  32.     struct sockaddr_in sock;
  33.     int s,i;
  34.     struct mbuf *bp;
  35.     struct timer *t;
  36.     time_t rtime;
  37.  
  38.     if((sock.sin_addr.s_addr = resolve(argv[1])) == 0){
  39.         log(-1,"RDATE:Host %s unknown\n",argv[1]);
  40.         return 1;
  41.     }
  42.     sock.sin_family = AF_INET;
  43.     sock.sin_port = IPPORT_TIME;
  44.  
  45.     /* Open connection to time server */
  46.     if((s = socket(AF_INET,SOCK_STREAM,0)) == -1) {
  47.         log(-1,"RDATE:Can't create socket");
  48.         return 1;
  49.     }
  50.     sockmode(s,SOCK_BINARY);
  51.  
  52.     /* Set timeout timer */
  53.     t = (struct timer *)callocw(1, sizeof(struct timer));
  54.     t->func = rdate_timeout;
  55.     t->arg = (void *) &s;
  56.     set_timer(t,RDATETIMEOUT*1000L);
  57.     start_timer(t);
  58.  
  59.     /* Connect to time server */
  60.     if(connect(s,(char *)&sock,sizeof(sock)) == -1){
  61.         /* Connect failed */
  62.         stop_timer(t);
  63.         free(t);
  64.         log(s,"RDATE: Connect failed");
  65.         close_s(s);
  66.         return 1;
  67.     }
  68.     /* Successful connect, stop timout timer */
  69.     stop_timer(t);
  70.     free(t);
  71.     /* Get time info sent by server */
  72.     recv_mbuf(s,&bp,0,NULLCHAR,(int *)0);
  73.     rtime = (time_t)pull32(&bp);
  74.     /* Convert 1900 start date to 1970 start date */
  75.     rtime -= DIFFTIME;
  76.     /* Set the system time */
  77.     stime(&rtime);
  78.     log(s,"RDATE:Clock set to - %s from %s\n",ctime(&rtime),argv[1]);
  79.     printf("Clock set to - %s\n",ctime(&rtime));
  80.     free_q(&bp);
  81.     close_s(s);
  82.     return 0;
  83. } /* dordate */
  84.  
  85. /*
  86.  * Close socket after timeout on connect
  87.  */
  88. static void
  89. rdate_timeout(arg)
  90.  
  91. void *arg;
  92. {
  93.     int     *s;
  94.  
  95.     s = (int *)arg;
  96.     close_s(*s);
  97. } /* rdate_timeout */
  98.